package com.aizhixin.lab.resource.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.course.entity.CourseAssist;
import com.aizhixin.lab.course.course.entity.CourseChapterAtt;
import com.aizhixin.lab.course.course.entity.CourseChapterFile;
import com.aizhixin.lab.course.course.entity.CourseFile;
import com.aizhixin.lab.course.template.entity.TemplateAssist;
import com.aizhixin.lab.course.template.entity.TemplateChapterFile;
import com.aizhixin.lab.course.template.entity.TemplateFile;
import com.aizhixin.lab.resource.domain.*;
import com.aizhixin.lab.resource.entity.DirFile;
import com.aizhixin.lab.resource.service.AssistService;
import com.aizhixin.lab.resource.service.CourseWareService;
import com.aizhixin.lab.resource.service.DirService;
import com.aizhixin.lab.resource.service.VideoService;
import com.aizhixin.lab.task.domain.QueryDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/resource")
@Api(value = "资源管理api", description = "统计资源、查看资源")
public class resourceController {
    @Autowired
    private CourseWareService courseWareService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private DirService dirService;
    @Autowired
    private AssistService assistService;
    @Autowired
    private VideoService videoService;

    @RequestMapping(value = "/getCollegeList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "学院列表", httpMethod = "POST", notes = "学院列表<br><br><b>lwq</b>")
    public ResponseEntity<?> getList(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "QueryDomain", required = false) @RequestBody QueryDomain domain) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        if (domain.getType() == null || 0 == domain.getType()) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "文件类型不能为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }
        result = courseWareService.getCollegeList(domain);

        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getProfList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取专业列表", httpMethod = "POST", notes = "获取专业列表<br><br><b>lwq</b>")
    public ResponseEntity<?> getProfList(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "QueryDomain", required = false) @RequestBody QueryDomain domain) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(domain.getId())) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "学院id不可以为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }
        if (domain.getType() == null || 0 == domain.getType()) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "文件类型不能为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }

        result = courseWareService.getProfessionalList(domain);

        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getCourseList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取课程名称和文件夹列表", httpMethod = "POST", notes = "获取课程和文件夹列表<br><br><b>lwq</b>")
    public ResponseEntity<?> getCourseList(@RequestHeader("Authorization") String token,
                                           @ApiParam(value = "QueryDomain", required = false) @RequestBody QueryDomain domain) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(domain.getId())) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "专业id不可以为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }
        if (domain.getType() == null || 0 == domain.getType()) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "文件类型不能为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }

        result = courseWareService.findCourseList(domain, account.getId());

        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "搜索", httpMethod = "POST", notes = "搜索<br><br><b>lwq</b>")
    public ResponseEntity<?> search(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "QueryDomain", required = false) @RequestBody QueryDomain domain) {

        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }

        if (domain.getType() == null || 0 == domain.getType()) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "文件类型不能为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }

        if (domain.getType() == 60) {
            result = assistService.searchAssist(domain);

        } else if (domain.getType() == 10) {

            result = courseWareService.searchCourseFile(domain);
        } else {
            result = courseWareService.searchFile(domain);
        }


        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);

    }

    @RequestMapping(value = "/getFileList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取文件列表", httpMethod = "POST", notes = "获取文件列表<br><br><b>lwq</b>")
    public ResponseEntity<?> getFileList(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "QueryDomain", required = false) @RequestBody QueryDomain domain) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(domain.getId())) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "课程id不可以为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }
        if (domain.getType() == null || 0 == domain.getType()) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "文件类型不能为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }

        String[] s = domain.getId().split("_");
        if (domain.getType() == 60) {
            if ("D".equals(s[0])) {
                result = courseWareService.findDirFile(domain);
            }
            if ("T".equals(s[0])) {
                result = assistService.getTemplateAssist(domain);
            }
            if ("C".equals(s[0])) {
                result = assistService.getCourseAssist(domain);
            }

        } else if (domain.getType() == 10) {
            if ("D".equals(s[0])) {
                result = courseWareService.findDirFile(domain);
            }
            if ("T".equals(s[0])) {
                result = courseWareService.findCourseTemplateFile(domain);
            }
            if ("C".equals(s[0])) {
                result = courseWareService.findCourseFile(domain);
            }
        } else {
            if ("D".equals(s[0])) {
                result = courseWareService.findDirFile(domain);
            }
            if ("T".equals(s[0])) {
                result = courseWareService.findTemplateFile(domain);
            }
            if ("C".equals(s[0])) {
                if (domain.getType() == 20) {
                    result = videoService.findVideoFile(domain);
                } else {
                    result = courseWareService.findFile(domain);
                }
            }
        }

        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findFileTotal", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取各种类型的文件总数", httpMethod = "GET", notes = "获取各种类型的文件总数<br><br><b>lwq</b>")
    public ResponseEntity<?> findFileTotal(@RequestHeader("Authorization") String token) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }

        List<FileTotalNumDomain> fileTotal = courseWareService.findFileTotal();
        FileTotalNumDomain fileTotalNumDomain = fileTotal.get(0);
        fileTotalNumDomain.setAssistFileNum(assistService.getFileTotalNumDomain());
        fileTotalNumDomain.setCourseFileNum(fileTotalNumDomain.getCourseFileNum() + courseWareService.getCourseFileNum());
//        if (fileTotalNumDomain1.getType()==10){
//            fileTotalNumDomain1.setFileNum(fileTotalNumDomain1.getFileNum()+courseWareService.getCourseFileNum());
//            fileTotal.set(0,fileTotalNumDomain1);
//        }

        return new ResponseEntity<>(fileTotalNumDomain, HttpStatus.OK);
    }

    @RequestMapping(value = "/createFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "创建文件夹", httpMethod = "POST", notes = "创建文件夹")
    public ResponseEntity<?> createFile(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "profId:专业id,folderName:文件夹名字", required = true) @RequestBody CreateFileDirDomain dirDomain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        try {
            Long createdBy = account.getId();
            dirService.createFolder(dirDomain, createdBy);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/getFolderAndCourse", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取一级列表课程名和文件夹名", httpMethod = "GET", notes = "获取当前专业下的所有课程名和文件夹名")
    public ResponseEntity<?> getVideoFolderAndCourse(@RequestHeader("Authorization") String token,
                                                     @ApiParam(value = "profId:专业id", required = false) @RequestParam(value = "profId", required = true) Long profId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        List<DirDomain> videoFolderDomainList = null;
        try {
            videoFolderDomainList = courseWareService.getFolderDomain(profId);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }
        return new ResponseEntity<>(videoFolderDomainList, HttpStatus.OK);
    }

    @RequestMapping(value = "/getLabChapter", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取二级章列表", httpMethod = "GET", notes = "获取二级章列表")
    public ResponseEntity<?> getVideoChapter(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "folderNoteId:文件夹标识id", required = false) @RequestParam(value = "folderNoteId", required = true) String folderNoteId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        List<CourseChapterDomain> videoChapterDomainList = null;
        try {
            videoChapterDomainList = courseWareService.getVideoChapterDomain(folderNoteId);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }
        return new ResponseEntity<>(videoChapterDomainList, HttpStatus.OK);
    }

    @RequestMapping(value = "/getSection", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取三级节列表", httpMethod = "GET", notes = "获取三级章列表")
    public ResponseEntity<?> getVideoSection(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "chapterNoteId:章标识id", required = false) @RequestParam(value = "chapterNoteId", required = true) String chapterNoteId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        List<CourseChapterDomain> videoSectionDomain = null;
        try {
            videoSectionDomain = courseWareService.getVideoSectionDomain(chapterNoteId);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }
        return new ResponseEntity<>(videoSectionDomain, HttpStatus.OK);
    }

    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传文件", httpMethod = "POST", notes = "上传文件")
    public ResponseEntity<?> fileUpload(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "folderNoteId:文件夹标识id,chapterNoteId:章标识id,sectionNoteId:节标识id,fileName:文件名,fileUrl:文件路径", required = false) @RequestBody FileUploadDomain domain) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(domain.getFolderNoteId())) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "文件夹标识不能为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }
        try {
            courseWareService.uploadFile(domain);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/EditFileName", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑", httpMethod = "PUT", notes = "编辑")
    public ResponseEntity<?> EditFileName(@RequestHeader("Authorization") String token,
                                          @ApiParam(value = "fileId:文件标识id", required = false) @RequestParam(value = "fileId") String fileId,
                                          @ApiParam(value = "name:文件名字", required = false) @RequestParam(value = "name", required = true) String name,
                                          @ApiParam(value = "type:文件类型", required = false) @RequestParam(value = "type", required = true) Integer type) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(fileId)) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "文件标识不能为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }
        if (StringUtils.isEmpty(name)) {
            result.put(ApiReturnConstants.SUCCESS, Boolean.FALSE);
            result.put(ApiReturnConstants.ERROR, "文件名称不能为空不能为空");
            return new ResponseEntity<Map<String, Object>>(result, HttpStatus.EXPECTATION_FAILED);
        }
        try {
            String[] split = fileId.split("_");
            if ("C".equals(split[0])) {
                Long videoId = Long.parseLong(split[1]);
                if (type == 60) {
                    CourseAssist courseAssist = assistService.updateCourseAssist(videoId, name);
                    return new ResponseEntity<>(courseAssist, HttpStatus.OK);
                }
                CourseChapterFile courseChapterFile = courseWareService.updateCourseChapterFile(videoId, name);
                return new ResponseEntity<>(courseChapterFile, HttpStatus.OK);
            } else if ("T".equals(split[0])) {
                Long videoTemplateId = Long.parseLong(split[1]);
                if (type == 60) {
                    TemplateAssist templateAssist = assistService.updateTemplateAssist(videoTemplateId, name);
                    return new ResponseEntity<>(templateAssist, HttpStatus.OK);
                }
                TemplateChapterFile templateChapterFile = courseWareService.updateTemplateChapterFile(videoTemplateId, name);
                return new ResponseEntity<>(templateChapterFile, HttpStatus.OK);
            } else if ("A".equals(split[0])) {
                Long courseId = Long.parseLong(split[1]);
                CourseFile courseFile = courseWareService.updateCourseFile(courseId, name);
                return new ResponseEntity<>(courseFile, HttpStatus.OK);
            } else if ("B".equals(split[0])) {
                Long templateId = Long.parseLong(split[1]);
                TemplateFile templateFile = courseWareService.updateTemplateFile(templateId, name);
                return new ResponseEntity<>(templateFile, HttpStatus.OK);
            } else if ("E".equals(split[0])) {
                Long videoId = Long.parseLong(split[1]);
                CourseChapterAtt chapterAtt = videoService.updateAttFileName(videoId, name);
                return new ResponseEntity<>(chapterAtt, HttpStatus.OK);
            } else {
                Long videoFileId = Long.parseLong(split[1]);
                DirFile dirFile = courseWareService.updateDirFile(videoFileId, name);
                return new ResponseEntity<>(dirFile, HttpStatus.OK);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }

    }


    @RequestMapping(value = "/deleteFile", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除文件", httpMethod = "DELETE", notes = "删除文件")
    public ResponseEntity<?> deleteFile(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "fileNoteId:文件标识id", required = true) @RequestParam(value = "fileNoteId", required = true) String fileNoteId,
                                        @ApiParam(value = "type:文件类型", required = true) @RequestParam(value = "type", required = true) Integer type) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        try {
            if (type == 60) {
                assistService.deleteAssistFile(fileNoteId);
            } else {
                courseWareService.deleteFile(fileNoteId);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/getFileUrl", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查看", httpMethod = "GET", notes = "获取文件路径")
    public ResponseEntity<?> getVideoUrl(@RequestHeader("Authorization") String token,
                                         @ApiParam(value = "fileNoteId:文件标识id", required = true) @RequestParam(value = "fileNoteId", required = true) String fileNoteId,
                                         @ApiParam(value = "type:文件类型", required = true) @RequestParam(value = "type", required = true) Integer type) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        try {
            String[] split = fileNoteId.split("_");
            if (type == 60) {
                if ("C".equals(split[0])) {
                    Long fileId = Long.parseLong(split[1]);
                    CourseAssist courseAssist = assistService.getCourseAssist(fileId);
                    if (courseAssist == null) {
                        return new ResponseEntity<>("文件不存在或已删除", HttpStatus.UNAUTHORIZED);
                    }
                    return new ResponseEntity<>(courseAssist, HttpStatus.OK);
                } else if ("T".equals(split[0])) {
                    Long fileId = Long.parseLong(split[1]);
                    TemplateAssist templateAssist = assistService.getTemplateAssist(fileId);
                    if (templateAssist == null) {
                        return new ResponseEntity<>("文件不存在或已删除", HttpStatus.UNAUTHORIZED);
                    }
                    return new ResponseEntity<>(templateAssist, HttpStatus.OK);
                }
            }
            if ("C".equals(split[0])) {
                Long fileId = Long.parseLong(split[1]);
                CourseChapterFile courseChapterFile = courseWareService.getCourseChapterFile(fileId);
                if (courseChapterFile == null) {
                    return new ResponseEntity<>("文件不存在或已删除", HttpStatus.UNAUTHORIZED);
                }
                return new ResponseEntity<>(courseChapterFile, HttpStatus.OK);
            } else if ("T".equals(split[0])) {
                Long fileId = Long.parseLong(split[1]);
                TemplateChapterFile templateChapterFile = courseWareService.getTemplateChapterFile(fileId);
                if (templateChapterFile == null) {
                    return new ResponseEntity<>("文件不存在或已删除", HttpStatus.UNAUTHORIZED);
                }
                return new ResponseEntity<>(templateChapterFile, HttpStatus.OK);
            } else if ("A".equals(split[0])) {
                Long fileId = Long.parseLong(split[1]);
                CourseFile templateChapterFile = courseWareService.findCourseFile(fileId);
                if (templateChapterFile == null) {
                    return new ResponseEntity<>("文件不存在或已删除", HttpStatus.UNAUTHORIZED);
                }
                return new ResponseEntity<>(templateChapterFile, HttpStatus.OK);
            } else if ("B".equals(split[0])) {
                Long fileId = Long.parseLong(split[1]);
                TemplateFile templateChapterFile = courseWareService.findTemplateFile(fileId);
                if (templateChapterFile == null) {
                    return new ResponseEntity<>("文件不存在或已删除", HttpStatus.UNAUTHORIZED);
                }
                return new ResponseEntity<>(templateChapterFile, HttpStatus.OK);
            } else if ("E".equals(split[0])) {
                Long fileId = Long.parseLong(split[1]);
                CourseChapterAtt attFile = videoService.findAttFile(fileId);
                if (attFile == null) {
                    return new ResponseEntity<>("文件不存在或已删除", HttpStatus.UNAUTHORIZED);
                }
                return new ResponseEntity<>(attFile, HttpStatus.OK);
            } else {
                Long fileId = Long.parseLong(split[1]);
                DirFile dirFile = courseWareService.getDirFile(fileId);
                if (dirFile == null) {
                    return new ResponseEntity<>("文件不存在或已删除", HttpStatus.UNAUTHORIZED);
                }
                return new ResponseEntity<>(dirFile, HttpStatus.OK);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }
    }
}
